package com.lwb.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwb.commonutils.Row;
import com.lwb.eduservice.entity.EduTeacher;
import com.lwb.eduservice.entity.vo.TeacherQuery;
import com.lwb.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-10
 */
//@RestController 里面包含 @Controller 和 @ResponseBody
// 主要作用是将这个类交给spring去管理,然后让返回值为JSON数据
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    //注入service
    @Autowired
    private EduTeacherService eduTeacherService;

    //查询讲师表所有数据
    @ApiOperation("查询讲师表所有数据")
    @GetMapping("findAll")
    public Row findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return Row.SUCCESS().data("list",list);
    }

    //讲师逻辑删除
    @ApiOperation("讲师逻辑删除")
    @DeleteMapping("{id}")//通过路径传id值
    public Row removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){//@PathVariable通过路径获取ID值
        boolean flag = eduTeacherService.removeTeacher(id);
        if(flag) {
            return Row.SUCCESS();
        }else {
            return Row.ERROR();
        }
    }

    //模糊查询讲师表所有数据分页
    @ApiOperation("模糊查询讲师表所有数据分页")
    @PostMapping("findPageTeacher/{current}/{limit}")
    public Row findPageTeacher(@ApiParam(name = "current", value = "当前页",required = false) @PathVariable Long current,
                               @ApiParam(name = "limit", value = "每页记录数",required = false) @PathVariable Long limit,
                               @RequestBody(required = false) TeacherQuery teacherQuery
                               ){
        System.out.println(teacherQuery.toString());
        //创建一个分页对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //创建一个条件对象
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(teacherQuery.getName())){
            //模糊查询
            queryWrapper.like("name",teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())){
            queryWrapper.le("gmt_create",teacherQuery.getEnd());
        }
        //用封装好的page方法进行分页查询，并且将数据放入page中
        eduTeacherService.page(pageTeacher,queryWrapper);
        List<EduTeacher> list = pageTeacher.getRecords();
        System.out.println(list.size());
        //创建DATA数据区
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageTeacher.getTotal());//总记录数
        map.put("records",pageTeacher.getRecords());//数据记录

        return Row.SUCCESS().data(map);
    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public Row addTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                              @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if(flag) {
            return Row.SUCCESS();
        }else{
            return Row.ERROR();
        }
    }

    //依据ID查询讲师
    @ApiOperation("依据ID查询讲师")
    @GetMapping("findById/{id}")
    public Row findById(@ApiParam(name = "id", value = "讲师ID", required = true)
                            @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return Row.SUCCESS().data("teacher",teacher);
    }

    //修改讲师
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public Row updateTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                                 @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateTeacher(eduTeacher);
        if (flag){
            return Row.SUCCESS();
        }else {
            return Row.ERROR();
        }
    }

}

